package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.dto.RegionDto;
import com.lkd.entity.Node;
import com.lkd.entity.Region;
import com.lkd.mapper.RegionMapper;
import com.lkd.service.INodeService;
import com.lkd.service.IRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * <p>
 * 运营区域 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-10-10
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Autowired
    private INodeService nodeService;

    @Override
    public PageBean<Region> search(RegionDto regionDto) {

        Page<Region> page = new Page<>(regionDto.getPageIndex(),regionDto.getPageSize());

        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<Region>().like(Region::getName, regionDto.getName());

        Page<Region> page1 = this.page(page, wrapper);

        page1.getRecords().stream().map( item ->{
            LambdaQueryWrapper<Node> eq = new LambdaQueryWrapper<Node>().eq(Node::getRegionId, item.getId());
            long count = nodeService.count(eq);
            item.setNodeCount(count);
            return item;
        }).collect(Collectors.toList());
        return new PageBean<>(page1);
    }
}
